package com.tentative.competition.schedule.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tentative.competition.common.core.result.PageResult;
import com.tentative.competition.common.model.schedule.param.AwardAddParam;
import com.tentative.competition.common.model.schedule.resp.*;
import com.tentative.competition.common.model.schedule.vo.FileVo;
import com.tentative.competition.schedule.domain.ScheduleAward;
import com.tentative.competition.schedule.domain.ScheduleOrgInfo;
import com.tentative.competition.schedule.mapper.ScheduleAwardMapper;
import com.tentative.competition.schedule.service.ScheduleAwardService;
import com.tentative.competition.schedule.service.ScheduleFileInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 赛程奖项配置 服务实现类
 * </p>
 *
 * @author SmileH
 * @since 2023-06-22
 */
@Service
public class ScheduleAwardServiceImpl extends ServiceImpl<ScheduleAwardMapper, ScheduleAward> implements IService<ScheduleAward>, ScheduleAwardService {

    @Lazy
    @Autowired
    private ScheduleFileInfoService fileInfoService;

    @Override
    public PageResult<AwardResp> pageList(AwardAddParam param) {
        IPage<AwardResp> infoResps = baseMapper.pageList(param,new Page<>(param.getPageNum(),param.getPageSize()));
        PageResult<AwardResp> pageResult = new PageResult<>();
        pageResult.setContent(infoResps.getRecords());
        pageResult.setTotal(infoResps.getTotal());
        return pageResult;
    }

    @Override
    public AwardResp getInfo(Long id) {
        AwardAddParam param  = new AwardAddParam();
        param.setId(id);
        List<AwardResp> awardResps = baseMapper.pageList(param,new Page<>(param.getPageNum(),param.getPageSize())).getRecords();
        if (CollUtil.isNotEmpty(awardResps)){
            return awardResps.get(0);
        }else {
            throw new RuntimeException("无法找到对象！");
        }
    }

    @Override
    public void addOrEdit(AwardAddParam param) {
        ScheduleAward award = new ScheduleAward();
        BeanUtil.copyProperties(param,award);
        this.saveOrUpdate(award);
    }

    @Override
    public Boolean deleteOrgInfo(List<Long> ids) {
        if (CollUtil.isEmpty(ids)){
            throw new RuntimeException("没有需要删除的数据！");
        }
        return this.lambdaUpdate()
                .in(ScheduleAward::getId,ids)
                .remove();
    }

    @Override
    public List<AwardsConfiResp> getAwardsConfig(Long scheduleId) {
        List<AwardsConfiResp> list = CollUtil.newArrayList();
        List<ScheduleAward> awards = this.lambdaQuery()
                .eq(ScheduleAward::getScheduleId, scheduleId)
                .orderByAsc(ScheduleAward::getSort)
                .list();
        if (CollUtil.isEmpty(awards)){
            return list;
        }
        List<AwardsConfiResp> resps = awards.stream().map(x -> {
            AwardsConfiResp resp = new AwardsConfiResp();
            BeanUtil.copyProperties(x,resp);
            return resp;
        }).collect(Collectors.toList());
//        Set<Long> logoFileIds = resps.stream().map(AwardsConfiResp::getAwardImg).collect(Collectors.toSet());
//        if (CollUtil.isNotEmpty(logoFileIds)){
//            Map<Long, FileVo> longFileVoMap = fileInfoService.selectFileMapByIds(logoFileIds);
//            resps.forEach(x->x.setAwardImgUrl(longFileVoMap.get(x.getAwardImg()).getFileUrl()));
//        }
        return resps;
    }

    @Override
    public List<GeneralMainResp> pullDown(Long scheduleId) {
        List<ScheduleAward> awards = this.lambdaQuery()
                .eq(ScheduleAward::getScheduleId, scheduleId)
                .orderByAsc(ScheduleAward::getSort)
                .list();
        if (CollUtil.isEmpty(awards)){
            return new ArrayList<>();
        }
        return awards.stream().map(x->{
            GeneralMainResp resp = new GeneralMainResp();
            resp.setKey(x.getId().toString());
            resp.setVal(x.getAwardName());
            return resp;
        }).collect(Collectors.toList());
    }
}
